
package com.example.testtimeserver;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;

import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.ChannelFactory;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;

import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.concurrent.Executors;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        startServer();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    @Override
    protected void onDestroy() {
        stopServer();
        super.onDestroy();
    }

    ServerBootstrap mBootstrap = null;

    private void startServer() {
        ChannelFactory factory =
                              new NioServerSocketChannelFactory(
                                      Executors.newSingleThreadExecutor(new PriorityThreadFactory(
                                              "boss_main")),
                                      Executors.newSingleThreadExecutor(new PriorityThreadFactory(
                                              "worker_sub")));

        mBootstrap = new ServerBootstrap(factory);

        TimeServerHandler handler = new TimeServerHandler();
        ChannelPipeline pipeline = mBootstrap.getPipeline();
        pipeline.addLast("handler", handler);

        mBootstrap.setOption("child.tcpNoDelay", true);
        mBootstrap.setOption("child.keepAlive", true);
        mBootstrap.bind(new InetSocketAddress(getLocalIpAddress(), 8080));

    }

    private void stopServer() {
        if (mBootstrap != null) {
            mBootstrap.releaseExternalResources();
        }
    }

    public InetAddress getLocalIpAddress() {
        try {
            for (Enumeration<NetworkInterface> en = NetworkInterface
                    .getNetworkInterfaces(); en.hasMoreElements();) {
                NetworkInterface intf = en.nextElement();
                for (Enumeration<InetAddress> enumIpAddr = intf
                        .getInetAddresses(); enumIpAddr.hasMoreElements();) {
                    InetAddress inetAddress = enumIpAddr.nextElement();
                    if (!inetAddress.isLoopbackAddress()) {
                        Log.e("inetAddress", "inetAddress = " + inetAddress);
                        return inetAddress;
                    }
                }
            }
        } catch (SocketException ex) {
            Log.e("WifiPreference IpAddress", ex.toString());
        }
        return null;
    }
}
